﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;

namespace CourseSystem.Stat
{
    public class UrlStat
    {
        public static Dictionary<int, UrlStatModel> StudentUrlStatDic;
        public static Dictionary<int, UrlStatModel> TeacherUrlStatDic;

        static UrlStat()
        {
            Init();
        }

        private static void Init()
        {
            StudentUrlStatDic = new Dictionary<int, UrlStatModel>();
            TeacherUrlStatDic = new Dictionary<int, UrlStatModel>();

        }

        /// <summary>
        /// 更新统计信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="url"></param>
        public static void UpdateStuUrlStat(int id, string url)
        {
            url = url.ToLower();
            var now = DateTime.Now;
            //加此判断性能更好
            if (UserHelper.Student.IsCanUpLastUrl)
                UpdateLastUrl(id, url);
            //新来的统计信息
            if (!StudentUrlStatDic.ContainsKey(id))
            {
                StudentUrlStatDic.Add(id, new UrlStatModel
                                              {
                                                  Id = UserHelper.LoginUser.Id,
                                                  UserType = 1,
                                                  Name = UserHelper.LoginUser.UserName,
                                                  Url = url,
                                                  StartViewTime = now,
                                                  LastUpdateTime = now,
                                                  UrlName = UrlMapping.StudentStatUrl[url]
                                              });
            }
            else
            {
                var info = StudentUrlStatDic[id];
                if (info.Url == url)
                    info.LastUpdateTime = now;
                else
                {
                    //地址发生变更
                    UrlChangeSubmit(info);
                    info.Url = url;
                    info.StartViewTime = now;
                    info.LastUpdateTime = now;
                    info.UrlName = UrlMapping.StudentStatUrl[url];
                }
            }
        }

        /// <summary>
        /// 用户直接关闭浏览器后马上重新进入
        /// 因轮询时间未过,所以因手动提交未提交的信息
        /// </summary>
        public static void LoginSafeCheck(bool setLogOut = false)
        {
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Guess)
                return;
            var id = UserHelper.LoginUser.Id;
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Student)
            {
                UrlStatModel info;
                if (StudentUrlStatDic.TryGetValue(id, out info))
                {
                    //更新数据库
                    UrlChangeSubmit(info, true);
                    UpdateLastUrl(info.Id, info.Url);
                }
                if (setLogOut)
                    StudentBLL.SetLogout(id);
                //统计学习进程
                LearnProcessStat.UpdateUser(id);
                return;
            }
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Teacher)
            {
                UrlStatModel info;
                if (TeacherUrlStatDic.TryGetValue(id, out info))
                {
                    //更新数据库
                    UrlChangeSubmit(info, true);
                }
                return;
            }
        }

        private static int GetTeaStatType(int type)
        {
            return type + 1;
        }

        public static void UpdateTeaUrlStat(int id, string url)
        {
            url = url.ToLower();
            var now = DateTime.Now;
            //新来的统计信息
            if (!TeacherUrlStatDic.ContainsKey(id))
            {
                TeacherUrlStatDic.Add(id, new UrlStatModel
                {
                    Id = UserHelper.LoginUser.Id,
                    UserType = GetTeaStatType(UserHelper.LoginUser.MinType),
                    Name = UserHelper.LoginUser.UserName,
                    Url = url,
                    StartViewTime = now,
                    LastUpdateTime = now,
                    UrlName = UrlMapping.TeacherStatUrl[url]
                });
            }
            else
            {
                var info = TeacherUrlStatDic[id];
                if (info.Url == url)
                    info.LastUpdateTime = now;
                else
                {
                    //地址发生变更
                    UrlChangeSubmit(info);
                    info.Url = url;
                    info.StartViewTime = now;
                    info.LastUpdateTime = now;
                    info.UrlName = UrlMapping.TeacherStatUrl[url];
                }
            }
        }

        public static void UpdateLastUrl(int stuId, string url)
        {
            StudentBLL.UpdateLastUrl(stuId, url);
        }

        public static void UrlChangeSubmit(UrlStatModel info, bool isNeedRemove = false)
        {
            if (info == null)
                return;
            var now = DateTime.Now;
            var add = new UrlStatInfo
            {
                StartViewTime = info.StartViewTime,
                EndViewTime = now,
                Url = info.Url,
                UserName = info.Name,
                UserType = info.UserType,
                ViewSecTicks = (now - info.StartViewTime).Minutes,
                UserId = info.Id,
                UrlName = info.UrlName
            };
            UrlStatBLL.Add(add);
            if (isNeedRemove)
            {
                if (info.UserType == 1)
                    StudentUrlStatDic.Remove(info.Id);
                else
                    TeacherUrlStatDic.Remove(info.Id);
            }
        }

        /// <summary>
        /// 保鲜
        /// </summary>
        /// <param name="id"></param>
        public static void RemainStuFlash(int id)
        {
            UrlStatModel info = null;
            if (StudentUrlStatDic.TryGetValue(id, out info))
            {
                info.LastUpdateTime = DateTime.Now;
            }
        }

        /// <summary>
        /// 保鲜
        /// </summary>
        /// <param name="id"></param>
        public static void RemainTeaFlash(int id)
        {
            UrlStatModel info = null;
            if (TeacherUrlStatDic.TryGetValue(id, out info))
            {
                info.LastUpdateTime = DateTime.Now;
            }
        }
    }
}
